package com.hnevc.xiaoming.shijianactivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.hnevc.xiaoming.R;

public class AddGesture extends AppCompatActivity {

    private GestureOverlayView gov;
    private Gesture mGesture;
    private ImageView show;
    private EditText gesture_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_gesture);
        initView();
        initData();
        initEvent();

    }

    private void initEvent() {

        //设置手绘的事件监听，手势完成后，触发。
        gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {


            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                mGesture = gesture;

                //加载save_gesture.xml界面布局视图
                View dialog = getLayoutInflater().inflate(R.layout.save_gesture, null);

                show = dialog.findViewById(R.id.show);
                gesture_name = dialog.findViewById(R.id.gesture_name);

                //根据Gesture包含的手势创建一个视图
                Bitmap bitmap = gesture.toBitmap(128,128,10,0Xff0000ff);
                show.setImageBitmap(bitmap);

                //使用对话框显示dialog组件
                new AlertDialog.Builder(AddGesture.this).setView(dialog)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(AddGesture.this,
                                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                        1);
                            }
                        }).setNegativeButton("取消",null).show();
            }
        });
    }

    public void saveFile() {
        if (mGesture!=null){

            //获取指定文件对应的手势库
            GestureLibrary lib = GestureLibraries.fromFile("/sdcard/mygestures");
            lib.addGesture(gesture_name.getText().toString(),mGesture);
            boolean result = lib.save();//保存手势
            if (result) Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            else Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode==1){
            for (int i=0;i<permissions.length;i++){
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                    saveFile();
                }else {
                    Toast.makeText(this, ""+"权限"+permissions[i]+"申请失败，不能保存手势文件", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private void initData() {


    }

    @SuppressLint("ResourceAsColor")
    private void initView() {
        gov = (GestureOverlayView) findViewById(R.id.gov);
        gov.setGestureColor(android.R.color.holo_blue_bright);//设置手绘颜色
        gov.setGestureStrokeWidth(10);//设置手绘的宽度

    }
}